package pooller.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import pooller.dto.pool.PoolQuestionDto;
import pooller.dto.pool.QuestionVariantDto;

import java.util.Optional;

public class VariantCell extends Composite {
    interface VariantCellUiBinder extends UiBinder<VerticalPanel, VariantCell> {}
    private static VariantCellUiBinder uiBinder = GWT.create(VariantCellUiBinder.class);
    @UiField
    TextArea questionText;

    private QuestionVariantDto variantDto;

    public VariantCell() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("removeVariant")
    public void removeVariantClick(ClickEvent event) {
        removeFromParent();
    }

    public QuestionVariantDto getVariantDto() {
        Optional.ofNullable(variantDto).ifPresent(v -> v.setVariantText(questionText.getText()));
        return variantDto;
    }

    public void setVariantDto(QuestionVariantDto variantDto) {
        this.variantDto = variantDto;
        Optional.ofNullable(variantDto).ifPresent(v -> questionText.setText(v.getVariantText()));
    }
}
